package com.example.wishstar.viewmodel;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.example.wishstar.MyApplication;
import com.example.wishstar.model.Register;
import com.example.wishstar.repository.RegisterRepository;
import com.example.wishstar.utils.ApiResponse;
import com.example.wishstar.view.activity.LoginActivity;

import retrofit2.Response;

public class RegisterViewModel implements RegisterRepository.RegisterCallBack {
    private ObservableField<Register> registerObservableField;
    private Context context;
    private RegisterRepository registerRepository;

    public RegisterViewModel(Context context){
        this.context = context;
        Register register = new Register("", "", "", "", "");
        registerObservableField = new ObservableField<>();
        registerObservableField.set(register);

        registerRepository = new RegisterRepository(MyApplication.getApi());
    }

    public String getNickname() {
        return registerObservableField.get().nickname;
    }

    public void setNickname(String nickname) {
        registerObservableField.get().nickname = nickname;
    }

    public String getUserMail() {
        return registerObservableField.get().userMail;
    }

    public void setUserMail(String userMail) {
        registerObservableField.get().userMail = userMail;
    }

    public String getPassword() {
        return registerObservableField.get().password;
    }

    public void setPassword(String password) {
        registerObservableField.get().password = password;
    }

    public String getPassword2() {
        return registerObservableField.get().password2;
    }

    public void setPassword2(String password2) {
        registerObservableField.get().password2 = password2;
    }

    public String getCode() {
        return registerObservableField.get().code;
    }

    public void setCode(String code) {
        registerObservableField.get().code = code;
    }

    public void getCode(View view) {
        registerRepository.getCode(1, getUserMail(), this);
    }

    public void register(View view) {
        if(getNickname().equals("")) {
            Toast.makeText(context, "请输入用户名", Toast.LENGTH_LONG).show();
        } else if (getUserMail().equals("")) {
            Toast.makeText(context, "请输入邮箱", Toast.LENGTH_LONG).show();
        } else if (getCode().equals("")) {
            Toast.makeText(context, "请输入验证码", Toast.LENGTH_LONG).show();
        } else if (getPassword().equals("")) {
            Toast.makeText(context, "请输入密码", Toast.LENGTH_LONG).show();
        } else if(getPassword().equals(getPassword2())) {
            registerRepository.register(getCode(), getNickname(), getPassword(), getUserMail(), this);
        } else {
            Toast.makeText(context, "两次密码输入不一致", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void callback_getCode(Response<ApiResponse<String>> response) {
        if(response.body().code == 200) {
            Toast.makeText(context, "验证码发送成功", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, response.body().getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void callback_register(Response<ApiResponse<Integer>> response) {
        Toast.makeText(context, response.body().getMessage(), Toast.LENGTH_SHORT).show();
        if(response.body().code == 200) {
            Intent intent = new Intent(context, LoginActivity.class);
            context.startActivity(intent);
        }
    }
}
